<?php
namespace app\serverauth\service\role;

use \app\manage\service\ApiBase;
/**
 * 对角色管理的操作
 * @author adv30 <adv30@126.com> \app\serverauth\service\role\Role
 */
class Role extends ApiBase
{
    protected $roleValidate = '\app\serverauth\validate\Role';
    protected $roleLogic = '\app\serverauth\logic\Role';
    // 增加一个角色
    public function add(): array
    {
        $data = $this->haveIssetReceiveData(['name' => ' ', 'name_cn' => ' ', 'weight' => '0']);
        return $this->oneStepRun($data, 'serveruser/auth/Role/add', 'add', 'add');
    }
    // 停用一个角色
    public function stop(): array
    {
        $data = $this->issetReceiveData(['id' => '0', 'name' => ' ']);
        return $this->oneStepRun($data, 'serveruser/auth/Role/stop', 'stop', 'stop');
    }
    // 启用一个角色
    public function start(): array
    {
        $data = $this->issetReceiveData(['id' => '0', 'name' => ' ']);
        return $this->oneStepRun($data, 'serveruser/auth/Role/start', 'start', 'start');
    }
    // 获取角色列表分页数据
    public function list(): array
    {
        $data = $this->haveIssetReceiveData(['page' => 1, 'list_rows' => '', 'status' => ''], []);
        return $this->oneStepRun($data, 'serveruser/auth/Role/list', 'list', 'list');
    }
    // 获取角色列表分页数据
    public function edit(): array
    {
        $data = $this->haveIssetReceiveData(['id' => '0']);
        $judge = isset($this->arrData['name']) 
                || isset($this->arrData['name_cn']) 
                || isset($this->arrData['weight']) 
                || isset($this->arrData['menu']);
        if ($judge) {
            isset($this->arrData['name']) ? $data['name'] = $this->arrData['name'] : null;
            isset($this->arrData['name_cn']) ? $data['name_cn'] = $this->arrData['name_cn'] : null;
            isset($this->arrData['weight']) ? $data['weight'] = $this->arrData['weight'] : null;
            isset($this->arrData['menu']) ? $data['menu'] = $this->arrData['menu'] : null;
        } else {
            return verify_err($data, 'name,name_cn,weight和menu四个参数至少要传一个');
        }
        return $this->oneStepRun($data, 'serveruser/auth/Role/edit', 'edit', 'edit');
    }
    // 用户申请某个角色
    public function apply(): array
    {
        $data = $this->haveIssetReceiveData(['app_id' => '0', 'user_id' => '0', 'role_id' => '0', 'apply_info' => '{}']);
        if ('{}' === $data['apply_info']) { 
            return verify_err(null, '请提交apply_info信息'); 
        }else if(is_array($data['apply_info'])) {
            $data['apply_info'] = json_encode($data['apply_info']); 
        }
        return $this->oneStepRun($data, 'serveruser/auth/Role/apply', 'apply', 'apply');
    }
    // 获取当前可以申请的角色
    public function allowApply(): array
    {
        $data = $this->haveIssetReceiveData(['app_id' => '0','user_id' => '0']);
        return $this->oneStepRun($data, 'serveruser/auth/Role/allowApply', 'allowApply', 'allowApply');
    }
    // 审核用户申请的角色
    public function audit(): array
    {
        $data = $this->haveIssetReceiveData(['id' => '0','pass_user_id' => '0']);
        return $this->oneStepRun($data, 'serveruser/auth/Role/audit', 'audit', 'audit');
    }
    // 给某一个用户某角色
    public function giveRoles(): array
    {
        $data = $this->haveIssetReceiveData(['id' => '', 'name' => '', 'phone' => '', 'role_id'=>'0']);
        if(array_eq($data, '', ['name','id','phone'])){
            return verify_err($data, 'name,id,phone至少要传一个');
        }
        return $this->oneStepRun($data, 'serveruser/user/User/giveRoles', 'giveRoles', 'giveRoles');
    }
    // 获取不分页的所有角色列表
    public function all(): array
    {
        return $this->oneStepRun([], 'serveruser/auth/Role/all', 'all');
    }
    // 获取某一应用下的所有的角色列表
    public function allRolesByApp(): array
    {
        $data = $this->haveIssetReceiveData(['id' => '', 'name' => '', 'app_key' => '']);
        if (array_eq($data, '', ['id', 'name', 'app_key'])) {
            return verify_err($data, 'id,name,app_key三个参数至少传一个');
        }
        return $this->oneStepRun($data, 'serveruser/auth/Role/allRolesByApp', 'allRolesByApp');
    }
    // 移除某用户的某角色
    public function removeUserRole(): array
    {
        $data = $this->haveIssetReceiveData(['id' => '', 'name' => '', 'phone' => '', 'role_id'=>'0']);
        if(array_eq($data, '', ['name','id','phone'])){
            return verify_err($data, 'name,id,phone至少要传一个');
        }
        return $this->oneStepRun($data, 'serveruser/user/User/removeUserRole', 'removeUserRole');
    }
    // 获取某个角色的详情
    public function getRoleInfo(): array 
    {
        $data = $this->haveIssetReceiveData(['id'=>0]);
        return $this->oneStepRun($data, 'serveruser/auth/Role/getRoleInfo');
    }
}